//-----------------------------------------------------------------------

// <copyright file="IntegerSpinnerFieldDefinition.cs" company="Jamie Knight">

// Copyright (c) Jamie Knight. All rights reserved.

// </copyright>

//-----------------------------------------------------------------------

using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;

namespace DevKnightlie.WPF.AutoForm.FieldDefinitions
{
    /// <summary>
    /// Field Definition class which builds a text box and "spinner" buttons
    /// </summary>
    public class IntegerSpinnerFieldDefinition:BaseFieldDefinition
    {
        #region Private Fields

        private int _minimum = 0;
        private int _maximum = 10;
        TextBox _textBox;
        RepeatButton _upButton;
        RepeatButton _downButton;

        #endregion

        #region Properties

        /// <summary>
        /// Sets or returns the minimum allowable value
        /// </summary>
        public int MinimumValue {
            get { return _minimum; }
            set { _minimum = value; }
        }


        /// <summary>
        /// Sets or returns the maximum value
        /// </summary>
        public int MaximumValue
        {
            get { return _maximum; }
            set { _maximum = value; }
        }


        /// <summary>
        /// Returns a reference to the TextBox
        /// </summary>
        public TextBox TextBox { get { return _textBox; } }


        /// <summary>
        /// Returns a reference to the "+" button
        /// </summary>
        public RepeatButton UpButton { get { return _upButton; } }


        /// <summary>
        /// Returns a reference to the "-" button
        /// </summary>
        public RepeatButton DownButton { get { return _downButton; } }

        #endregion

        #region Methods

        public override void BuildInputControl()
        {
            ContentControl result = new ContentControl()
            {
                Focusable = false,
                VerticalAlignment = System.Windows.VerticalAlignment.Stretch
            };
            DockPanel fieldPanel = new DockPanel()
            {
                VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
                LastChildFill = true
            };
            _textBox = new TextBox()
            {
                HorizontalContentAlignment = System.Windows.HorizontalAlignment.Right,
                VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch
            };
            Grid spinnerGrid = new Grid()
            {
                Width = 24,
                Height=26,
                Focusable = false
            };
            spinnerGrid.ColumnDefinitions.Add(new ColumnDefinition());
            spinnerGrid.RowDefinitions.Add(new RowDefinition());
            spinnerGrid.RowDefinitions.Add(new RowDefinition());
            _upButton = new RepeatButton()
            {
                Content = "+",
                VerticalAlignment=System.Windows.VerticalAlignment.Stretch,
                HorizontalAlignment=System.Windows.HorizontalAlignment.Stretch, 
                FontSize=8
            };
            _upButton.Click += upButton_Click;
            _downButton = new RepeatButton()
            {
                Content = "-",
                VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
                FontSize=8
            };
            _downButton.Click += downButton_Click;

            // Bindings
            Binding textValueBinding = new Binding(PropertyName)
            {
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };
            BindingOperations.SetBinding(_textBox, TextBox.TextProperty, textValueBinding);
            SetupEnabledBinding(_textBox);
            SetupEnabledBinding(_upButton);
            SetupEnabledBinding(_downButton);

            spinnerGrid.Children.Add(_upButton);
            spinnerGrid.Children.Add(_downButton);
            Grid.SetRow(_upButton, 0);
            Grid.SetRow(_downButton, 1);
            fieldPanel.Children.Add(spinnerGrid);
            DockPanel.SetDock(spinnerGrid, Dock.Right);
            fieldPanel.Children.Add(_textBox);
            result.Content = fieldPanel;

            ContainerControl = result;
            InputControl = _textBox;

        }

        void downButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            int value = 0;
            if (int.TryParse(_textBox.Text, out value))
            {
                if (value > MinimumValue)
                {
                    value -= 1;
                    _textBox.Text = value.ToString();
                }
            }
        }

        void upButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            int value = 0;
            if (int.TryParse(_textBox.Text, out value))
            {
                if (value < MaximumValue)
                {
                    value += 1;
                    _textBox.Text = value.ToString();
                }
            }
        }

        #endregion

    }
}
